package strauss.flatland.view;

import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import strauss.flatland.control.FlatLandControl;
import strauss.flatland.model.FlatLandModel;

public class FlatLandGUI extends JFrame implements WindowListener {
	private AbstractFlatLandComponent component;
	private FlatLandControl control;
	/**
	 * 
	 */
	private static final long serialVersionUID = 5019097114491120386L;
	
	public FlatLandGUI(FlatLandModel model, FlatLandControl control) {
		// so we can listen to window events
		addWindowListener(this);
		// create the gui component
		component = new FlatLandComponent();
		// give the gui component a model, so it can render it
		component.setModel(model);
		// mush the component onto the frame
		add(component);
		// keep a reference to the control
		this.control=control;
	}
	public static void createAndShowGUI(FlatLandModel model, FlatLandControl control) {
		FlatLandGUI frame = new FlatLandGUI(model, control);
		
		Rectangle posSize = new Rectangle(0, 0, 300, 300);
		posSize.x = frame.getGraphicsConfiguration().getBounds().width / 2
				- posSize.width / 2;
		posSize.y = frame.getGraphicsConfiguration().getBounds().height / 2
				- posSize.height / 2;
		frame.setBounds(posSize);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setVisible(true);
		// start the universe!
		control.startUniverse();
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void windowClosing(WindowEvent arg0) {
		control.terminateUniverse();
		component.stop();
	}
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
